﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;

namespace Agresso.Interface.Layer.Domain
{
    public class jPOAttachments
    {
        public long jPOAttachmentsID = 0;
        public string PoNo = string.Empty;
        public string FileName = string.Empty;
        public string FileType = string.Empty;
        public byte[] BlobContents = null;
        public int BlobSize = 0;
        public DateTime AddedOn = new DateTime(1900, 1, 1);

        public jPOAttachments(SqlDataReader rd)
        {
            jPOAttachmentsID = rd["jPOAttachmentsID"] is DBNull ? 0 : Convert.ToInt64(rd["jPOAttachmentsID"]);
            PoNo = rd["PoNo"] is DBNull ? "" : rd["PoNo"].ToString();
            FileName = rd["FileName"] is DBNull ? "" : rd["FileName"].ToString();
            FileType = rd["FileType"] is DBNull ? "" : rd["FileType"].ToString();
            BlobContents = rd["BlobContents"] is DBNull ? null : (byte[])rd["BlobContents"];
            BlobSize = rd["BlobSize"] is DBNull ? 0 : Convert.ToInt32(rd["BlobSize"]);
            AddedOn = rd["AddedOn"] is DBNull ? AddedOn : Convert.ToDateTime(rd["AddedOn"]);
        }
        public void WriteFile(string folder)
        {
            if (BlobContents == null) return;
            using (FileStream fs = new FileStream(folder + FileName + FileType, FileMode.Create))
            {
                fs.Write(BlobContents, 0, BlobContents.Length);
                fs.Close();
            }
        }
    }
}
